package com.example.video_module;

import android.content.Context;
import android.graphics.Color;
import android.text.TextUtils;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewbinding.ViewBinding;

import com.blankj.utilcode.util.ToastUtils;
import com.bw.ijk.widget.IjkView;
import com.example.adapter_sdk.quick.BaseQuickMoreAdapter;
import com.example.common.mvvm.view.BaseFragment;
import com.yanyusong.y_divideritemdecoration.Y_Divider;
import com.yanyusong.y_divideritemdecoration.Y_DividerBuilder;
import com.yanyusong.y_divideritemdecoration.Y_DividerItemDecoration;
import java.util.List;

public class VideoFragment extends BaseFragment<com.example.video_module.databinding.FragmentVideoBinding,VideoViewModel>   {
    @Override
    public View bindLayout() {
        return v.getRoot();
    }

    @Override
    public ViewBinding initBinding() {
        com.example.video_module.databinding.FragmentVideoBinding binding = com.example.video_module.databinding.FragmentVideoBinding.inflate(inflater);
        return binding;
    }

    @Override
    public void initView() {

    }

    @Override
    public void initData() {
        My_DividerItemDecoration my_dividerItemDecoration = new My_DividerItemDecoration(getContext());
        vm.videoList.observe(this, new Observer<List<VideoEntity.DataBean.VideoData>>() {
            @Override
            public void onChanged(List<VideoEntity.DataBean.VideoData> videoData) {
                VideoAdapter videoAdapter = new VideoAdapter();
                LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
                layoutManager.setOrientation(RecyclerView.VERTICAL);
                v.recycler.setLayoutManager(layoutManager);
                v.recycler.addItemDecoration(new My_DividerItemDecoration(getContext()));
                v.recycler.addOnScrollListener(new RecyclerView.OnScrollListener() {
                    @Override
                    public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
                        super.onScrollStateChanged(recyclerView, newState);
                        if (newState==RecyclerView.SCROLL_STATE_IDLE){
                            LinearLayoutManager recyclerManager= (LinearLayoutManager) recyclerView.getLayoutManager();
                            int firstPosition=recyclerManager.findFirstCompletelyVisibleItemPosition();
                            View viewByPosition = recyclerManager.findViewByPosition(firstPosition);
                            if (viewByPosition!=null){
                                IjkView ijkView = viewByPosition.findViewById(R.id.item_video_video_background);
                                String url=videoData.get(firstPosition).url;
                                if (!TextUtils.isEmpty(url)&&ijkView!=null){
                                    ijkView.setPlayUrl(url);
                                }
                            }
//                            惯性或拖动
                        }else if (newState==RecyclerView.SCROLL_STATE_SETTLING||newState==RecyclerView.SCROLL_STATE_DRAGGING){
                            LinearLayoutManager recyclerManager = (LinearLayoutManager) recyclerView.getLayoutManager();
                            for (int i = 0; i < recyclerView.getAdapter().getItemCount(); i++) {
                                View view = recyclerManager.findViewByPosition(i);
                                if (view!=null){
                                    IjkView ijkView = view.findViewById(R.id.item_video_video_background);
//                                    if (ijkView.isPlaying())
//                                        ijkView.stop();
                                }
                            }
                        }
                    }

                    @Override
                    public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                        super.onScrolled(recyclerView, dx, dy);
                    }
                });
                v.recycler.setAdapter(videoAdapter);

                videoAdapter.setNewInstance(videoData);
            }
        });
    }

//    @Override
//    public void onItemChildClick(BaseQuickMoreAdapter adapter, int id, int position) {
//        if (id == R.id.like) {
//
//        }
//    }


    public class My_DividerItemDecoration extends Y_DividerItemDecoration{

        public My_DividerItemDecoration(Context context) {
            super(context);
        }

        @Override
        public Y_Divider getDivider(int itemPosition) {
            return new Y_DividerBuilder()
                    .setTopSideLine(false, Color.parseColor("#F4F3F4"),10, 0,0)
                    .setLeftSideLine(false, Color.parseColor("#F4F3F4"),10, 0,0)
                    .setBottomSideLine(true, Color.parseColor("#F4F3F4"),10, 0,0)
                    .setRightSideLine(false, Color.parseColor("#F4F3F4"),10, 0,0)
                    .create();
        }
    }
}
